import { Component, OnInit, ViewChild } from '@angular/core';
import { STColumn, STComponent, STData } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFSchemaEnum, SFSelectWidgetSchema, SFUISchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { map } from 'rxjs/operators';
import { OrderManagementService } from '../../services/order-management.service';
import { of } from 'rxjs';
import { SearchDrawerService, ShipperBaseService } from '@shared';
import { Router } from '@angular/router';
import { BasicTableComponent } from 'src/app/routes/commom';

@Component({
  selector: 'app-order-management-abnormal-warning',
  templateUrl: './abnormal-warning.component.html',
  styleUrls: ['../../../commom/less/commom-table.less', './abnormal-warning.component.less']
})
export class OrderManagementAbnormalWarningComponent extends BasicTableComponent implements OnInit {
  uiView: SFUISchema = {};
  schemaView: SFSchema = {};
  changeId: any; // 主页面查看运费变更记录id - 用于运费变更记录
  changeViewId: any; // 查看运费变更记录id - 用于查看
  auditId: any;
  auditIdR: any;
  auditMany = false;
  isVisibleView = false;
  isVisibleEvaluate = false;
  isVisible = false;
  isVisibleRE = false;
  @ViewChild('sfView', { static: false }) sfView!: SFComponent;
  @ViewChild('stFloat') private readonly stFloat!: STComponent;
  @ViewChild('stFloatView') private readonly stFloatView!: STComponent;
  columns: STColumn[] = [];
  columnsFloat: STColumn[] = [];
  columnsFloatView: STColumn[] = [];
  ViewCause: any; // 变更运费查看数据
  constructor(
    public service: OrderManagementService,
    private modal: NzModalService,
    public shipperservice: ShipperBaseService,
    private router: Router,
    public searchDrawerService: SearchDrawerService
  ) {
    super(searchDrawerService);
  }

  /**
   * 查询参数
   */
  get reqParams() {
    const a: any = {};
    const params: any = Object.assign({}, this.sf?.value || {});
    delete params._$expand;
    return {
      ...a,
      ...params,
      warningTime: {
        start: this.sf?.value?.warningTime?.[0] || '',
        end: this.sf?.value?.warningTime?.[1] || ''
      }
    };
  }
  get changeParams() {
    return {
      id: this.changeId
    };
  }
  get selectedRows() {
    return this.st?.list.filter(item => item.checked) || [];
  }
  get changeViewParams() {
    return {
      id: this.changeViewId
    };
  }
  search() {
    this.st?.load(1);
  }

  ngOnInit(): void {
    this.initSF();
    this.initST();
  }

  /**
   * 初始化查询表单
   */
  initSF() {
    this.schema = {
      properties: {
        _$expand: { type: 'boolean', ui: { hidden: true } },
        billCode: {
          type: 'string',
          title: '订单号',
          ui: {}
        },
        wayBillCode: {
          type: 'string',
          title: '运单号',
          ui: {}
        },
        serviceType: {
          title: '服务类型',
          type: 'string',
          default: '',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'service:type' },
            containsAllLabel: true
          } as SFSelectWidgetSchema
        },
        resourceType: {
          title: '货源类型',
          type: 'string',
          default: '',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'goodresource:type' },
            containsAllLabel: true
          } as SFSelectWidgetSchema
        },
        shipperId: {
          type: 'string',
          title: '货主',
          ui: {
            widget: 'select',
            serverSearch: true,
            searchDebounceTime: 300,
            searchLoadingText: '搜索中...',
            allowClear: true,
            onSearch: (q: any) => {
              let str = q.replace(/^\s+|\s+$/g, '');
              if (str) {
                return this.service
                  .request(this.service.$api_enterpriceList, { enterpriseName: str })
                  .pipe(map((res: any) => (res as any[]).map(i => ({ label: i.enterpriseName, value: i.id } as SFSchemaEnum))))
                  .toPromise();
              } else {
                return of([]);
              }
            }
          } as SFSelectWidgetSchema
        },
        loadingPlace: {
          type: 'string',
          title: '装货地',
        },
        dischargePlace: {
          type: 'string',
          title: '卸货地',
        },
        driverName: {
          title: '承运司机',
          type: 'string',
        },
        carNo: {
          title: '车牌号',
          type: 'string',
        },
        warningTime: {
          title: '预警时间',
          type: 'string',
          ui: {
            widget: 'date',
            mode: 'range',
            format: 'yyyy-MM-dd',
            allowClear: true,
          } as SFDateWidgetSchema
        }
      },
      type: 'object'
    };
  }

  /**
   * 初始化数据列表
   */
  initST() {
    this.columns = [
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      {
        title: '订单号',
        width: '180px',
        fixed: 'left',
        className: 'text-left',
        index: 'billCode'
      },
      {
        title: '运单号',
        width: '180px',
        fixed: 'left',
        className: 'text-left',
        index: 'wayCode'
      },
      {
        title: '服务类型',
        index: 'serviceTypeLabel',
        width: '220px',
        className: 'text-left',
        format: item => {
          return item?.resourceTypeLabel + item?.serviceTypeLabel;
        }
      },
      { title: '货主', index: 'shipperName', width: '220px', className: 'text-left' },
      { title: '装货地', index: 'loadingAddressArr', width: '220px', className: 'text-left' },
      { title: '卸货地', index: 'unloadingAddressArr', width: '220px', className: 'text-left' },
      { title: '司机', render: 'driverName', width: '180px', className: 'text-left' },
      { title: '车牌号', index: 'carNo', width: '180px', className: 'text-left' },
      { title: '预警类型', index: 'warningTypeLabel', width: '180px', className: 'text-left' },
      {
        title: '预警时间',
        className: 'text-left',
        width: '180px',
        index: 'warningTime'
      },
      {
        title: '位置描述',
        className: 'text-left',
        width: '250px',
        render: 'longitude'
      },
      {
        title: '提醒内容',
        className: 'text-left',
        width: '250px',
        index: 'warningContent'
      }
    ];
  }

  tabChange(item: any) {}

  // 导出
  exprot() {
    this.service.exportStart({ ...this.reqParams, pageSize: -1 }, this.service.$api_abnormalWarning_asyncExport);
  }
}
